﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Pub;
using Meteorologicalservice.Office;
using ICSharpCode.TextEditor.Document;
//using Raize.CodeSiteLogging;

namespace LZQXT.Test.Forms
{
    public partial class FDefVal : Form
    {
        ADOTest ado1;
        public string s_ID = "";
        string sDefVal_ID = "";


        public FDefVal()
        {
            InitializeComponent();
        }

        private void FDefVal_Load(object sender, EventArgs e)
        {
            panel1.Dock = DockStyle.Fill;
            tabControl1.Dock = DockStyle.Fill;
            tboxResult.Dock = DockStyle.Fill;
            tboxInfo.Dock = DockStyle.Fill;
            tboxVal.Dock = DockStyle.Fill;
            tboxValTest.Dock = DockStyle.Fill;

            CTextEditMenu tboxMenu1 = new CTextEditMenu(tboxVal);
            CTextEditMenu tboxMenu2 = new CTextEditMenu(tboxValTest);

            tboxVal.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("TSQL");
            tboxValTest.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("TSQL");

            tabControl1.SelectedIndex = 0;
            tabControl2.SelectedIndex = 0;

            CPub.webPath = Application.StartupPath + @"\";

            init();
        }

        public void init()
        {
            if (s_ID == null || s_ID == "") return;

            ado1 = Program.fdb_tree.ado1;
            dataGridView1.DataSource = null;

            string strSQL = "SELECT * FROM P_DEFAULTVALUE "
                + " WHERE ID IN(SELECT SOURCE FROM P_Product_GridPropert WHERE F_ID={0}) "
                + " OR ID IN(SELECT DefaultValue FROM P_Product_MainPropert WHERE F_ID={0}) ";

            strSQL = strSQL.Replace("{0}", s_ID);

            try
            {
                DataSet ds1 = ado1.sel1(strSQL);
                //CDebug.Send("返回结果个数: " + ado1.ds_count);
                dataGridView1.DataSource = ds1.Tables[0];
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
            }

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i_Row = e.RowIndex;
            if (i_Row == -1) return;

            if (dataGridView1.RowCount == 0) return;

            sDefVal_ID = dataGridView1.Rows[i_Row].Cells[0].Value.ToString();
            string sVal = dataGridView1.Rows[i_Row].Cells[4].Value.ToString();
            tboxVal.Text = sVal;

            //显示变量替换后的值
            CDefaultVal.sqlReplace(ref sVal, dateTimePicker1.Value, "", 0);
            tboxValTest.Text = sVal;

            try
            {
                tboxResult.Text = CDefaultVal.getDefaultVal(Convert.ToInt32(sDefVal_ID), dateTimePicker1.Value, "");
                tboxInfo.Text = "";
            }
            catch (System.Exception ex)
            {
                tboxInfo.Text = ex.ToString();
                CDebug.SendError(ex.ToString());
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int i_btn = Convert.ToInt32(((Button)sender).Tag);

            try
            {
                switch (i_btn)
                {
                    //update
                    case 1:
                        string strSQL = "UPDATE P_DEFAULTVALUE SET Val='" + tboxVal.Text.Replace("'", "''") + "' WHERE ID=" + sDefVal_ID;
                        CDebug.Send(strSQL);
                        if (CPub.MsgBox(strSQL, "确定要更新数据库吗?"))
                            ado1.cmd1(strSQL);
                        break;

                    //refresh DB
                    case 2:
                        init();
                        break;

                    //refresh Value
                    case 3:
                        string sVal = tboxVal.Text;
                        CDefaultVal.sqlReplace(ref sVal, dateTimePicker1.Value, "", 0);
                        tboxValTest.Text = sVal;
                        break;

                }
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
            }

        }


    }
}
